﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;

namespace Mengsk.ProxyDelivery.App.Domain
{
    public class ColorType
    {
        const string NUMBERS = "0123456789ABCDEF";

        public int Id { get; set; }

        public string Name { get; set; }

        public Color Color { get; set; }

        public ColorType(string name, int r, int g, int b)
        {
            this.Name = name;
            this.Color = Color.FromArgb(255, (byte)r, (byte)g, (byte)b);
        }

        private int ParseColor(string color)
        {
            if (color.Length < 2)
            {
                color += color;
            }

            int high = NUMBERS.IndexOf(color[0]);
            int low = NUMBERS.IndexOf(color[1]);

            if (high < 0 || low < 0)
            {
                throw new Exception("颜色值不正确" + color);
            }

            return (high << 4) + low;
        }

        public ColorType(string name, string colors)
        {
            this.Name = name;
            colors = colors.Trim().Trim('#');
            int r = 0, g = 0, b = 0;

            if (colors.Length == 3)
            {
                r = ParseColor(colors[0].ToString());
                g = ParseColor(colors[1].ToString());
                b = ParseColor(colors[2].ToString());
            }
            else if (colors.Length == 6)
            {
                r = ParseColor(colors.Substring(0, 2));
                g = ParseColor(colors.Substring(2, 2));
                b = ParseColor(colors.Substring(4, 2));
            }
            else
            {
                throw new Exception("颜色值长度不正确" + colors);
            }
            this.Color = Color.FromArgb(255, (byte)r, (byte)g, (byte)b);
        }

        public ColorType()
        {
        }
    }
}
